package com.security.test.handle;

import com.alibaba.fastjson2.JSONObject;
import com.security.test.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class MySuccessHandler implements AuthenticationSuccessHandler {

    //    @Override
//    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authentication) throws IOException, ServletException {
//
//    }
    @Autowired
    private JwtUtil jwtUtil;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        System.out.println("MySuccessHandler");
        String s = authentication.getPrincipal().toString();
        String jwt = jwtUtil.createJWT(s);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("token", jwt);
        jsonObject.put("msg", "登陆成功");
        response.getWriter().write(jsonObject.toJSONString());
    }
}
